package com.example.exam.interceptor;

import com.example.exam.logInfo.service.LogInfoService;
import com.example.exam.shiro.ShiroUtils;
import com.example.exam.sysPermission.entity.SysPermissionBean;
import com.example.exam.sysPermission.service.SysPermissionService;
import com.example.exam.sysResource.model.SysResourceBean;
import com.example.exam.sysResource.service.SysResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Component
public class TestInterceptor implements HandlerInterceptor {
    @Autowired
    LogInfoService ls;
    @Autowired
    SysResourceService sysResourceService;
    @Autowired
    SysPermissionService  sysPermissionService;
    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
    }
    @Override
        /**
            * @Description:后置拦截，当username为空跳转至首页
            * @Param: [request, response, handler]
            * @return: boolean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        String token = ShiroUtils.getToken();
        if(!StringUtils.isNotEmpty(token)){
            response.sendRedirect("/user/toLogin");
            return false;
        }else{
            String urlString = request.getRequestURI();
            String[] urlSplit = urlString.split("/");
            if(urlSplit[1].contains("user")||urlSplit[1].contains("main")){
                urlSplit[1]="SysUser";
            }else if(urlSplit[1].contains("logInfoController")||urlSplit[1].contains("logInfo")){
                urlSplit[1]="SysLog";
            }else if(urlSplit[1].contains("sysDictInfoController")||urlSplit[1].contains("sysDictInfo")){
                urlSplit[1]="SysDict";
            }else if(urlSplit[1].contains("sysRoleController")||urlSplit[1].contains("sysRole")){
                urlSplit[1]="SysRole";
            }else if(urlSplit[1].contains("sysResource")){
                urlSplit[1]="SysResource";
            }else if(urlSplit[1].contains("staff")){
                urlSplit[1]="SysStaff";
            }else if(urlSplit[1].contains("post")){
                urlSplit[1]="SysPost";
            }
            String methodName=null;
            if(urlSplit.length==3){
                methodName=urlSplit[2];
            }else{
                methodName=null;
            }
            if("toUser".equals(methodName)||"toSysRole".equals(methodName)||"selectRole".equals(methodName)||"toSysResource".equals(methodName)||"toResourceList".equals(methodName)
                    ||"toParent".equals(methodName)||"toSysDictInfo".equals(methodName)||"selectDict".equals(methodName)||"findParentDict".equals(methodName)
                    ||"queryParentResource".equals(methodName)||"queryResource".equals(methodName)||"toDtreePage".equals(methodName)||
                    "toPost".equals(methodName)){
                methodName="toSelect";
            }else if ("toAddUser".equals(methodName)||"insertUser".equals(methodName)||"toAddRole".equals(methodName)||"insertRole".equals(methodName)||
                    "toAddResource".equals(methodName)||"insertResource".equals(methodName)||"toAddDict".equals(methodName)||"insertDict".equals(methodName)||"add".equals(methodName)
                    ||"toAdd".equals(methodName)){
                methodName="toAdd";
            }else if ("updateUser".equals(methodName)||"toUpdate".equals(methodName)||"selectOne".equals(methodName)||"productPwd".equals(methodName)||"toUpdateRole".equals(methodName)||
                    "updateRole".equals(methodName)||"toEditResource".equals(methodName)||"updateResource".equals(methodName)||"toUpdateDict".equals(methodName)||
                    "updateDict".equals(methodName)||"selectOneDict".equals(methodName)||"edit".equals(methodName)||"toEdit".equals(methodName)||"restPwd".equals(methodName)){
                methodName="toEdit";
            }else if ("deleteUser".equals(methodName)||"toDelete".equals(methodName)||"deleteRole".equals(methodName)||
                    "deleteResource".equals(methodName)||"deleteDict".equals(methodName)||"delete".equals(methodName)||"toDelete".equals(methodName)){
                methodName="toDelete";
            }else if ("toGrant".equals(methodName)||"grant".equals(methodName)||"index".equals(methodName)||"getTreeList".equals(methodName)||"savePersmission".equals(methodName)||"saveGrant".equals(methodName)){
                methodName="toGrant";
            }
            List<SysResourceBean> resource = sysResourceService.queryResourceByPermission(urlSplit[1]);
            if (resource != null) {
                for (SysResourceBean sysResourceBean : resource){
                    if("toSelect".equals(methodName)||"toAdd".equals(methodName)||"toDelete".equals(methodName)||"toEdit".equals(methodName)||"toGrant".equals(methodName)){
                        SysPermissionBean permission = sysPermissionService.queryPermission(methodName, sysResourceBean.getId());
                        Integer userId = ShiroUtils.getUserId();
                        if(sysPermissionService.isHasPermission(userId,sysResourceBean.getId(),permission.getId())){
                        }else{
                            response.sendRedirect("/user/toOut");
                        }
                    }
                }
            }
            return true;
        }
    }
}
